package com.example.recordingpen_datacollection.controller;


import com.example.recordingpen_datacollection.entity.BaiduKey;
import com.example.recordingpen_datacollection.entity.Vo.BaiduKeyVo;
import com.example.recordingpen_datacollection.redisService.RedisService;
import com.example.recordingpen_datacollection.result.RLIST;
import com.example.recordingpen_datacollection.service.BaiduKeyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.example.recordingpen_datacollection.config.constant.ConstantName.SQLSERVER_BAIDU_KEY;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xg
 * @since 2024-06-14
 */
@Api(tags = "百度key")
@RestController
@RequestMapping("/baidu-key")
public class BaiduKeyController {
    @Autowired
    private BaiduKeyService baiduKeyService;

    @Autowired
    private RedisService redisService;

    @ApiOperation(value = "添加百度令牌路径")
    @PostMapping("setBadk")
    public RLIST setBadk(@RequestBody BaiduKeyVo baiduKeyVo, HttpServletRequest request){
        String token = request.getHeader("token");
        if( !(token.length() == 56) ){
            return RLIST.error().message("无权操作");
        }
        boolean judge = baiduKeyService.setBaidukey(baiduKeyVo);
        if(judge){
            return RLIST.ok();
        } else {
            return RLIST.error();
        }
    }
    @ApiOperation(value = "查询百度令牌路径")
    @PostMapping("getBadk")
    public RLIST getBadk(@RequestBody BaiduKeyVo baiduKeyVo, HttpServletRequest request){
        boolean keyData = redisService.getKeyData(SQLSERVER_BAIDU_KEY);
        if(keyData){
            return RLIST.ok().data("data",redisService.getValue(SQLSERVER_BAIDU_KEY));
        } else {
            return RLIST.error();
        }
    }

    @ApiOperation(value = "测试")
    @PostMapping("Textgetkey")
    public RLIST Textgetkey(HttpServletRequest request){
//        String token = request.getHeader("token");
//        if( !(token.length() == 56) ){
//            return RLIST.error().message("无权操作");
//        }
        return RLIST.ok().data("data",        baiduKeyService.payBDtoken()
        );
    }

    @ApiOperation(value = "删除Redis")
    @PostMapping("Textgetkey2")
    public RLIST Textgetkey2(HttpServletRequest request){
//        String token = request.getHeader("token");
//        if( !(token.length() == 56) ){
//            return RLIST.error().message("无权操作");
//        }
        boolean b = redisService.delKeyData(SQLSERVER_BAIDU_KEY);
        System.out.println(b);
       return RLIST.ok();
    }

    @ApiOperation(value = "数据库同步")
    @PostMapping("sasasa")
    public RLIST sasasa(HttpServletRequest request){

        boolean b = baiduKeyService.sqlScheduleTask();
        return RLIST.ok().data("data",b);
    }



}

